package com.moyun.finance.service;

import com.moyun.finance.entity.*;
import com.moyun.finance.repository.*;
import com.moyun.finance.utils.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContractService {
    @Autowired
    ContractRepository cr;
    @Autowired
    ReceivablesRepository rr;
    @Autowired
    ProductContractMidRepository pcmr;
    @Autowired
    OrgUserRepository our;
    @Autowired
    ProjectsRepository pr;
    @Autowired
    CustomerRepository cusr;

    public Response save(Contract c){
        c.setId(BaseUtil.uuid());
        List<Receivables> rs = c.getReceivables();
        List<ProductContractMid> pcms = c.getProductContractMids();
        //产品关系表储存
        for(ProductContractMid pcm : pcms){
            pcm.setId(BaseUtil.uuid());
            pcm.setContractId(c.getId());
            pcmr.save(pcm);
        }
        //应收付款储存
        for(Receivables r : rs){
            if(c.getContractType() == 0){//如果是采购合同 则应付
               r.setState(0);
            }else{
                r.setState(1);
            }
            r.setId(BaseUtil.uuid());
            r.setContractId(c.getId());
            r.setAlreadyMoney(0);
            r.setSurplusMoney(r.getReceivablesMoney());
            rr.save(r);
        }
        cr.save(c);
        return new Response("合同保存成功!",true);
    }

    public Response update(Contract c){
        cr.save(c);
        List<Receivables> rs = c.getReceivables();
        List<ProductContractMid> pcms = c.getProductContractMids();
        //产品关系表储存
        for(ProductContractMid pcm : pcms){
            if(pcm.getId() == null ||"".equals(pcm.getId())){
                pcm.setId(BaseUtil.uuid());
            }
            pcmr.save(pcm);
        }
        //应收款储存
        for(Receivables r : rs){
            if(r.getId() == null || "".equals(r.getId())){
                r.setId(BaseUtil.uuid());
            }
            rr.save(r);
        }
        return new Response("合同信息修改成功!",true);
    }

    public Response delete(Contract c){
        List<Receivables> rs = c.getReceivables();
        List<ProductContractMid> pcms = c.getProductContractMids();
        //产品关系表储存
        for(ProductContractMid pcm : pcms){
            pcmr.delete(pcm);
        }
        //应收款储存
        for(Receivables r : rs){
            rr.delete(r);
        }
        cr.delete(c);
        return new Response("合同删除成功",true);
    }

    //本公司所有合同列表展示
    public List<Contract> findAll(String companyId){
        List<Contract> contracts =  cr.findByCompanyId(companyId);
        for(Contract c : contracts){
            List<Receivables> rs = rr.findByContractIdOrderByReceivablesDate(c.getId());//本合同所有关联应收
            List<ProductContractMid> pcms = pcmr.findByContractId(c.getId());//本合同所有关联产品
            OrgUser user = our.findById(c.getLeadingId()).orElse(null);//合同签署人(负责人)信息
            Projects p = pr.findById(c.getProjectsId()).orElse(null);//合同关联项目
            Customer cus = cusr.findById(c.getCustomerId()).orElse(null);//合同关联客户
            c.setCustomer(cus);
            c.setProjects(p);
            c.setLeadingUser(user);
            c.setReceivables(rs);
            c.setProductContractMids(pcms);
        }
        return contracts;
    }

    //根据id详细信息
    public Contract findOneById(String contractId){
        Contract c = cr.findById(contractId).orElse(null);
        if(c != null){
            List<Receivables> rs = rr.findByContractIdOrderByReceivablesDate(c.getId());//本合同所有关联应收
            List<ProductContractMid> pcms = pcmr.findByContractId(c.getId());//本合同所有关联产品
            OrgUser user = our.findById(c.getLeadingId()).orElse(null);//合同签署人(负责人)信息
            Projects p = pr.findById(c.getProjectsId()).orElse(null);//合同关联项目
            Customer cus = cusr.findById(c.getCustomerId()).orElse(null);//合同关联客户
            c.setCustomer(cus);
            c.setProjects(p);
            c.setLeadingUser(user);
            c.setReceivables(rs);
            c.setProductContractMids(pcms);
        }
        return c;
    }

    //删除产品合同关联记录
    public Response deleteProduct(List<String> pcmIds){
        for(String pcmId : pcmIds){
            ProductContractMid pcm = pcmr.findById(pcmId).orElse(null);
            pcmr.delete(pcm);
        }
        return new Response("删除成功",true);
    }

    public List<Contract> findByType(String companyId, int type){
        List<Contract> contracts =  cr.findByCompanyIdAndContractType(companyId, type);
        for(Contract c : contracts){
            List<Receivables> rs = rr.findByContractIdOrderByReceivablesDate(c.getId());//本合同所有关联应收
            List<ProductContractMid> pcms = pcmr.findByContractId(c.getId());//本合同所有关联产品
            OrgUser user = our.findById(c.getLeadingId()).orElse(null);//合同签署人(负责人)信息
            Projects p = pr.findById(c.getProjectsId()).orElse(null);//合同关联项目
            Customer cus = cusr.findById(c.getCustomerId()).orElse(null);//合同关联客户
            c.setCustomer(cus);
            c.setProjects(p);
            c.setLeadingUser(user);
            c.setReceivables(rs);
            c.setProductContractMids(pcms);
        }
        return contracts;
    }

    //根据子类别查询
    public List<Contract> findByTypeAndAllType(String companyId, int type, String allTypeId){
        if(allTypeId == null || "".equals(allTypeId)){
            return findByType(companyId, type);
        }
        List<Contract> contracts =  cr.findByCompanyIdAndContractTypeAndAllTypeId(companyId, type, allTypeId);
        for(Contract c : contracts){
            List<Receivables> rs = rr.findByContractIdOrderByReceivablesDate(c.getId());//本合同所有关联应收
            List<ProductContractMid> pcms = pcmr.findByContractId(c.getId());//本合同所有关联产品
            OrgUser user = our.findById(c.getLeadingId()).orElse(null);//合同签署人(负责人)信息
            Projects p = pr.findById(c.getProjectsId()).orElse(null);//合同关联项目
            Customer cus = cusr.findById(c.getCustomerId()).orElse(null);//合同关联客户
            c.setCustomer(cus);
            c.setProjects(p);
            c.setLeadingUser(user);
            c.setReceivables(rs);
            c.setProductContractMids(pcms);
        }
        return contracts;
    }
}
